const gulp = require('gulp');
const less = require('gulp-less');
const browserSync = require('browser-sync');
// const nodemon = require('gulp-nodemon');
const localPort = 3005;
const browserSyncPort = 3006;

// less css js文件监视任务
gulp.task('less', () => {
    return gulp.src(['./static/less/*.less'])//多个文件以数组形式传入
        .pipe(less())
        .pipe(gulp.dest('./static/css'));
});
gulp.task('js', () => {
    return gulp.src('./static/js/*.js')
    // do stuff to JavaScript files
    //.pipe(uglify())
    //.pipe(gulp.dest('...'));
});
gulp.task('css', () => {
    return gulp.src('./static/css/*.css')
        // .pipe(browserSync.reload({ stream: true }));
})

gulp.task('watch', async () => {
    gulp.watch('./static/less/*.less', gulp.series('less'));
    gulp.watch('./static/**/*.css', gulp.series('css', 'bs-reload'));
    gulp.watch('./static/**/*.js', gulp.series('js', 'bs-reload'));
    gulp.watch('./*.html', gulp.series('bs-reload'));
    
})

// BrowserSync task:
gulp.task('browser-sync', async () => {
    // 等待后端加载完成再打开浏览器
    setTimeout(function() {
        return browserSync.init({
            // proxy: 'http://localhost:' + localPort + '',
            port: browserSyncPort,
            server: {
                baseDir: "./"
            }
        });
    }, 1500); 
});

gulp.task('bs-reload', async () => {
    return browserSync.reload();
});

// gulp.task('default', gulp.series('nodemon', 'watch', 'browser-sync'));
gulp.task('default', gulp.series('watch', 'browser-sync'));
